package com.shsmi.application;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.shsmi.util.NoodlesList;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.awt.geom.GeneralPath;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class NoodlesApi {
    /**
     * 判断点是否在面中
     *
     * @param x
     * @param y
     * @return
     */
    @GetMapping("/public/coordinatejudge")
    public Object coordinateJudge(@RequestParam("x") String x, @RequestParam("y") String y) {
        JSONArray jsonArray = addNoodlesData(x, y);
        return jsonArray;
    }


    @GetMapping("/public/coordinatejudgeList")
    public Object coordinateJudgeList(@RequestBody Map<String, Object> map) {
        Map retMap = new HashMap();
        for (Map.Entry<String, Object> entiy : map.entrySet()) {
            JSONArray jsonArray = addNoodlesData(JSONArray.parseArray(entiy.getValue().toString()).get(0).toString(),
                    JSONArray.parseArray(entiy.getValue().toString()).get(1).toString());
            retMap.put(entiy.getKey(), jsonArray);
        }
        return retMap;
    }

    public static JSONArray addNoodlesData(String x, String y) {
        JSONArray jsonArray = new JSONArray();
        for (JSONObject jsonObjStr : NoodlesList.committeeList) {
            List<GeneralPath> noodles = (List<GeneralPath>) jsonObjStr.get("noodlesList");
            for (GeneralPath generalPath : noodles) {
                Boolean boo = generalPath.contains(Double.parseDouble(x), Double.parseDouble(y));
                if (boo == true) {
                    JSONObject jsonObject = JSONObject.parseObject(jsonObjStr.get("attributes").toString());
                    jsonArray.add(jsonObject);
                    break;
                }
            }
        }
        for (JSONObject jsonObjStr : NoodlesList.streetList) {
            List<GeneralPath> noodles = (List<GeneralPath>) jsonObjStr.get("noodlesList");
            for (GeneralPath generalPath : noodles) {
                Boolean boo = generalPath.contains(Double.parseDouble(x), Double.parseDouble(y));
                if (boo == true) {
                    JSONObject jsonObject = JSONObject.parseObject(jsonObjStr.get("attributes").toString());
                    jsonArray.add(jsonObject);
                    break;
                }
            }
        }
        for (JSONObject jsonObjStr : NoodlesList.gridList) {
            List<GeneralPath> noodles = (List<GeneralPath>) jsonObjStr.get("noodlesList");
            for (GeneralPath generalPath : noodles) {
                Boolean boo = generalPath.contains(Double.parseDouble(x), Double.parseDouble(y));
                if (boo == true) {
                    JSONObject jsonObject = JSONObject.parseObject(jsonObjStr.get("attributes").toString());
                    jsonArray.add(jsonObject);
                    break;
                }
            }
        }
        for (JSONObject jsonObjStr : NoodlesList.polygonList) {
            List<GeneralPath> noodles = (List<GeneralPath>) jsonObjStr.get("noodlesList");
            for (GeneralPath generalPath : noodles) {
                Boolean boo = generalPath.contains(Double.parseDouble(x), Double.parseDouble(y));
                if (boo == true) {
                    JSONObject jsonObject = JSONObject.parseObject(jsonObjStr.get("attributes").toString());
                    jsonArray.add(jsonObject);
                    break;
                }
            }
        }
        return jsonArray;
    }
}
